# 五子棋界面

import tkinter as tk
from tkinter import messagebox

from gobang.chess import ChessBoard, ChessColor, GobangError

current_color = ChessColor.WHITE


def click(x, y):
    global current_color
    try:
        winner = chess_board.mark(x, y, current_color)
        buttons[x][y]['bg'] = '#FFFFFF' if current_color == ChessColor.WHITE else '#000000'

        if winner is not None:
            tk.messagebox.showinfo('提示', '白方胜' if winner == ChessColor.WHITE else '黑方胜')
        else:
            current_color = ChessColor.BLACK if ChessColor.WHITE == current_color else ChessColor.WHITE
    except GobangError as ge:
        tk.messagebox.showinfo('提示', ge.error_msg)


window = tk.Tk()
window.title("五子棋")
window.resizable(0, 0)

chess_board = ChessBoard()
length = chess_board.length
width = chess_board.width
buttons = [[None for i in range(length)] for j in range(width)]
for x in range(width):
    for y in range(length):
        button = tk.Button(window, width=3, height=1)
        button.grid(row=x, column=y)
        button['bg'] = '#8A8A8A'
        button['command'] = lambda x=x, y=y: click(x, y)
        buttons[x][y] = button

window.mainloop()
